package com.liu.config;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;
import java.io.IOException;

@Configuration
@PropertySource("classpath:db.properties")
@EnableTransactionManagement//启用事务注解支持
@ComponentScan("com.liu")
@MapperScan("com.liu.mapper")
@EnableWebMvc //启用WebMvc注解支持
public class DbConfig {

    @Value("${user}")
    String username;
    @Value("${password}")
    String password;
    @Value("${driver}")
    String driver;
    @Value("${url}")
    String url;

    /**
     * 数据源配置
     * @return
     */
    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driver);
        return dataSource;
    }

    /**
     * 事务管理器
     * @param dataSource
     * @return
     */
    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        //设置数据源
        factoryBean.setDataSource(dataSource);
        //设置别名,指定包下的类默认第一个字母小写
        factoryBean.setTypeAliasesPackage("com.liu.bean");


        //设置分页插件
        factoryBean.setPlugins(new PageInterceptor());


        //mybatis配置类
        org.apache.ibatis.session.Configuration cfg = new org.apache.ibatis.session.Configuration();
        //输出sql语句
        cfg.setLogImpl(StdOutImpl.class);
        factoryBean.setConfiguration(cfg);

        //设置映射文件的位置
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //读取类路径下的mybatis文件夹下 以Mapper.xml结尾的文件
        Resource [] resources = null;
        try {
            resources= resolver.getResources("classpath:mybatis/*Mapper.xml");
        }catch (IOException e){
            throw new RuntimeException(e);
        }
        factoryBean.setMapperLocations(resources);
        try {
            return factoryBean.getObject();
        }catch (Exception e){
            throw new RuntimeException(e);
        }


    }




}
